const { User } = require('../../model/user')
const bcrypt = require('bcryptjs');
module.exports = async(req, res) => {
    const { email, password } = req.body;
    if (email.trim() == '' || password.trim() == '') {
        return res.render('admin/error', { msg: '账号或者密码不能为空' });
    }
    let user = await User.findOne({ email: email });
    if (user) {
        let isEquals = await bcrypt.compare(password, user.password);
        if (isEquals) {
            req.session.username = user.username;
            req.session.role = user.role;
            req.app.locals.userInfo = user;
            res.redirect('/admin/user');
        } else {
            return res.render('admin/error', { msg: '账号或者密码错误' });
        }
    }
}